const API_BASE = 'https://tihaidengta.xyz';
// const API_BASE = 'http://172.20.10.14:8001';
// const { checkLoginAndActivation } = require('../../utils/auth');
Page({data: {
  theme: '',
  bookList: [], // 存储四本书的数组
  loading:true //骨架屏 
},
  onShow() {
  const app = getApp();
  this.setData({  
    theme: app.globalData.isDarkMode ? 'dark-mode' : '',
    loading:false

  });
   // 请求书籍数据
    this.fetchBookList();
},

fetchBookList() {
  const token = wx.getStorageSync('token') || '';
  wx.request({
    url: `${API_BASE}/eduservice/subjectBank/selectSubjectBank/1/4`,
    method: 'GET',
    header: {
      'content-type': 'application/json',
      'token': token // ✅ 传递 token 到请求头
    },
    success: (res) => {
      if (res.data.code === 20000) {
        this.setData({
          bookList: res.data.data.list
        });
      } else {
        wx.showToast({
          title: '获取题库失败',
          icon: 'none'
        });
      }
    },
    fail: (err) => {
      wx.showToast({
        title: '网络错误',
        icon: 'none'
      });
      console.error('请求失败', err);
    }
  });
},
  // 每日一练
  // goToPractice(e) {
  //   const bankId = e.currentTarget.dataset.bankid;

  //   checkLoginAndActivation()
  //     .then(() => {
  //       // 通过检测后再跳转
  //       wx.navigateTo({
  //         url: `/subpackage/practice/practice/practice?bankId=${bankId}`,
  //       });
  //     })
  //     .catch((err) => {
  //       console.warn('跳转被拦截:', err);
  //       // 登录/激活失败，request 中已处理跳转或弹窗，无需额外处理
  //     });
  // },
   // 每日一练
   goToPractice(e) {
    const bankId = e.currentTarget.dataset.bankid;
    wx.navigateTo({
      url: `/subpackage/practice/practice/practice?bankId=${bankId}`,
    });
  },
  // 错题集
  Notebook(){
    // wx.navigateTo({
    //   url: '/pages/Notebook/Notebook',
    // })
  },
  
  //智能练习
  // SmartPractice(){
  //   checkLoginAndActivation()
  //   .then(() => {
  //     wx.navigateTo({
  //       url: '/subpackage/practice/SmartPractice/SmartPractice',
  //     });
  //   })
  //   .catch((err) => {
  //     console.warn('跳转被拦截:', err);
  //     // 登录/激活失败，request 中已处理跳转或弹窗，无需额外处理
  //   });
  // },
// 智能练习
SmartPractice() {
  wx.navigateTo({
    url: '/subpackage/practice/SmartPractice/SmartPractice',
  });
},
  //收二手书
  ChapterExercises(){
    wx.navigateTo({
      url: '/subpackage/practice/ChapterExercises/ChapterExercises',
    })
  },
  //历年真题
  old(){
    wx.navigateTo({
      url: '/subpackage/other/old/old',
    })
  },
  //做题记录
  CQI(){
    wx.navigateTo({
      url: '/subpackage/other/CQI/CQI',
    })
  },
  //职业道德
  ZYDD(){
    wx.navigateTo({
      url: '/pages/ZYDD/ZYDD',
    })
  },
  //法规概论
  FGGL(){
    wx.navigateTo({
      url: '/pages/FGGL/FGGL',
    })
  },
  //心理学
  XLX(){
    wx.navigateTo({
      url: '/pages/XLX/XLX',
    })
  },
  //教育学
  JYX(){
    wx.navigateTo({
      url: '/pages/JYX/JYX',
    })
  },
  //错题练习
  goToWrong(){
    wx.navigateTo({
      url: '/subpackage/notebook/Wrong/Wrong',
    })
  },
  //模拟考试
  goToMockExams(){
    wx.navigateTo({
      url: '/subpackage/exam/MockExams/MockExams',
    })
  },
  //收藏夹
  goToFavorites(){
    wx.navigateTo({
      url: '/subpackage/notebook/Favorites/Favorites',
    })
  },
  goToBookPage(e) {
    const bankId = e.currentTarget.dataset.bankid;
    const bankName = e.currentTarget.dataset.name;

    wx.navigateTo({
      url: `/subpackage/exam/exam/exam?bankId=${bankId}&bankName=${encodeURIComponent(bankName)}`
    });
  },
  // 页面分享功能
  onShareAppMessage(){},
  onShareTimeline(){}

});